<?php
class GlobalAction extends BaseAction {
	
	private function __isValidRequest($field, $array = 'post') {
		$field = is_array($field) ? $field : explode(',', $field);
		$array = $array == 'post' ? $_POST : $_GET;
		foreach ($field as $v){
			$v = trim($v);
			if ( !isset($array[$v]) || $array[$v] == '' ) return false;
		}
		return true;
	}
	
	/** 系统配置 - 站点配置 **/
	
	//站点设置
	public function siteopt() {
		$site_opt = model('Xdata')->lget('siteopt');
		$this->assign($site_opt);

		//require_once ADDON_PATH . '/libs/Io/Dir.class.php';
    //    $theme_list = new Dir(SITE_PATH.'/public/themes/');
        //$this->assign('theme_list',$theme_list->toArray());
        $this->display();
	}
	
	//设置站点
	public function doSetSiteOpt() {
		if (empty($_POST)) {
			$this->error('参数错误');
		}
		
		$_POST['site_name']           		= t($_POST['site_name']);
		$_POST['site_slogan']		  		= t($_POST['site_slogan']);
		$_POST['site_header_keywords']		= t($_POST['site_header_keywords']);
		$_POST['site_header_description']	= t($_POST['site_header_description']);
		$_POST['site_closed']		 		= intval($_POST['site_closed']);
		$_POST['site_closed_reason'] 		= t($_POST['site_closed_reason']);
		$_POST['site_icp']  		 		= t($_POST['site_icp']);
		$_POST['site_verify']		 		= isset($_POST['site_verify']) ? $_POST['site_verify'] : '';
		
		$res = model('Xdata')->lput('siteopt', $_POST);
		if ($res) {
			$this->assign('jumpUrl', U('/Global/siteopt'));
			$this->success('保存成功');
		}else {
			$this->error('保存失败');
		}
	}
	
	/** 系统配置 - 注册配置 **/
	
	public function register() {
		$register = model('Xdata')->lget('register');
		$this->assign($register);
		$invite   = model('Invite')->getSet();
		$this->assign($invite);
		$this->display();
	}
	
	public function doSetRegisterOpt() {
		$invite_set['invite_set'] = t($_POST['invite_set']);
		unset($_POST['invite_set']);
		if ( model('Xdata')->lput('register', $_POST) && model('Xdata')->lput('inviteset', $invite_set) ) {
			$this->assign('jumpUrl', U('/Global/register'));
			$this->success('保存成功');
		}else {
			$this->error('保存失败');
		}
	}
	
	
	/** 系统配置 - 邀请配置 **/
	
	//邀请配置
	function invite(){
		$data = model('Invite')->getSet();
		$this->assign( $data );
		$this->display();
	}
	
	//邀请码发放
	function invitecode(){
		$num = intval($_POST['send_type_num']);
		$user = t($_POST['send_type_user']);
		if($_POST['send_type']==1){
			$user = M('user')->where('is_init=1 AND is_active=1')->field('uid')->findall();
			foreach ($user as $key=>$value){
				model('Invite')->sendcode($value['uid'],$num);
			}
		}else{
			$user = explode(',', $user);
			foreach ($user as $k=>$v){
				model('Invite')->sendcode($v,$num);
				x('Notify')->sendIn($v,'admin_sendinvitecode',array('num'=>$num)); //通知发送
			}					
		}
		$this->success('操作成功');
	}
	
	/** 系统配置 - 公告配置 **/
	
	public function announcement() {
		if ($_POST) {
			unset($_POST['__hash__']);
			model('Xdata')->lput('announcement',$_POST);
			$this->assign('jumpUrl', U('/Global/announcement'));
			$this->success('保存成功');
		}else {
			$announcement = model('Xdata')->lget('announcement');
			$this->assign($announcement);
			$this->display();
		}
	}
	
	/** 系统配置 - 邮件配置 **/
	public function email(){
		if($_POST){
			unset($_POST['__hash__']);
			model('Xdata')->lput('email',$_POST);
			$this->assign('jumpUrl', U('/Global/email'));
			$this->success('保存成功');
		}else{
			$email = model('Xdata')->lget('email');
			$this->assign($email);
			$this->display();
		}
	}
	
	
	/** 首页相关设置 **/
	public function hp(){
		$top_imgs = model('Xdata')->get('hpopt:top_imgs');
		$hpimgs = unserialize($top_imgs);
		$this->assign('hpimgs',$hpimgs);
		
		$recommend = model('Xdata')->get('hpopt:recommend');
		$shelf = unserialize($recommend);
		$product = service('Product')->getByShelfId($shelf['sid']);
		$this->assign('product',$product);
		
		$hot_sids  = model('Xdata')->get('hpopt:hot');
		if(!empty($hot_sids)){
			$hotProduct = service('Product')->selectByLimit(array( 'shelf_id'=>array( 'in' , explode(',', $hot_sids) ) ),'shelf_send_number desc',100);
			$this->assign('hotProduct',$hotProduct);
		}
		$this->assign('hotshelfids',$hot_sids);
		$this->display();
	}
	public function setHpRecommend(){
		$shelfid = $_POST['shelfid'];
		$opt = $_POST['opt'];
		if($opt=='set'){
			$product = service('Product')->getByShelfId($shelfid);
			if(empty($product)){
				$result['status']=0;
				exit(json_encode($result));
			}
			$data_['sid']=$shelfid;
			$data_['title']=$product['goods_title'];
			$data_['icon']=$product['goods_icon'];
			$data_['shelf_out_price']=$product['shelf_out_price'];
			$data['recommend']=serialize($data_);
			model('Xdata')->lput('hpopt',$data);
			$result['status']=1;
			//$result['data']=$product;
			exit(json_encode($result));
		}
		if($opt=='del'){
			$data['recommend']='';
			model('Xdata')->lput('hpopt',$data);
			$result['status']=1;
			exit(json_encode($result));
		}
		$result['status']=0;
		exit(json_encode($result));
	}
	
	public function setHpHot(){
		$shelfids = trim($_POST['shelfids']);
		if(!empty($shelfids)){
//			10006,10007,10019,10018,10017,10011,10010,10014,10015
			//echo $shelfids;
			//dump(explode(',', $shelfids));
			$hotProduct = service('Product')->selectByLimit(array( 'shelf_id'=>array( 'in' , explode(',', $shelfids) ) ),'shelf_send_number desc',100);
			//echo M('')->getLastSql();
			//dump($hotProduct);
			if(count($hotProduct)<8){
				$result['status']=1;
				$result['msg']='有效商品数不能小于8！';
				exit(json_encode($result));
			}
		}
		$data['hot']=$shelfids;
		model('Xdata')->lput('hpopt',$data);
		$result['status']=1;
		if(empty($shelfids))$result['msg']='删除热门商品设置成功！';
		else $result['msg']='热门商品设置成功！';
		exit(json_encode($result));
	}
	
	public function hpImgEdit(){
		$newimgpath = $_POST['newimgpath'];
		$newimghref = $_POST['newimghref'];
		if(!empty($newimgpath)){
			$top_imgs = model('Xdata')->get('hpopt:top_imgs');
			//unset($_POST['__hash__']);
			//echo $top_imgs;
			$datas = unserialize($top_imgs);
			//dump($datas);
			//if(empty($newimghref))$newimghref="#";
			$datas[] = array('img'=>$newimgpath,'href'=>$newimghref);
			//dump($datas);
			$data['top_imgs']=serialize($datas);
			model('Xdata')->lput('hpopt',$data);
			$result['status']=1;
			exit(json_encode($result));
		}
		$top_imgs = model('Xdata')->get('hpopt:top_imgs');
		$hpimgs = unserialize($top_imgs);
		$this->assign('hpimgs',$hpimgs);
		$this->display();
	}
	
	
	public function ajxEditHpImg(){
		$img_src = $_POST['img_src'];
		$img_href = $_POST['img_href'];
		$opt = $_POST['opt'];
		
		if(!empty($opt)){
			$result['status']=0;
			
			$top_imgs = model('Xdata')->get('hpopt:top_imgs');
			$datas = unserialize($top_imgs);
			if($opt=='top'){
				$index = 1;
				$datas_[0]['img']=$img_src;
				foreach($datas as $vo){
					if($img_src==$vo['img']){
						$datas_[0]=$vo;
					}else{
						$datas_[$index]=$vo;
						$index = $index+1;
					}
				}
			}
			if($opt=='del'){
				$index = 0;
				foreach($datas as $vo){
					if($img_src==$vo['img']){
						//$datas_[0]=$vo;
					}else{
						$datas_[$index]=$vo;
						$index = $index+1;
					}
				}
			}
			if($opt=='href'){
				$index = 0;
				foreach($datas as $vo){
					if($img_src==$vo['img']){
						$vo['href']=$img_href;
					}
					$datas_[$index]=$vo;
					$index = $index+1;
				}
			}
			if(!empty($datas_)){
				$data['top_imgs']=serialize($datas_);
				model('Xdata')->lput('hpopt',$data);
			}
			$result['status']=1;
			exit(json_encode($result));
		}
		$result['status']=0;
		exit(json_encode($result));
	}
	
	
	/** 系统配置 - 短信配置 **/
	public function sms(){
		$sms = model('Xdata')->lget('sms');
		//dump($sms);
		$this->assign('sms',$sms);
		$this->display();
	}
	
	/** 系统配置 - 短信配置 **/
	public function saveSms(){
		if($_POST){
			unset($_POST['__hash__']);
			$provider = $_POST['provider'];
			unset($_POST['provider']);
			$datastr = serialize($_POST);
			//echo $datastr;
			$data[$provider]=$datastr;
			model('Xdata')->lput('sms',$data);
			$this->assign('jumpUrl', U('/Global/sms'));
			$this->success('保存成功');
		}else{
			$provider = $_GET['provider'];
			if(!empty($provider)){
				$sms = model('Xdata')->get('sms:'.$provider);
				$this->assign('provider',$provider);
				$this->assign(unserialize($sms));
			}
			
			$this->display('editSms');
		}
	}
	
	/** 系统配置 - 短信配置 **/
	public function deleteSms(){
		$provider = $_GET['provider'];
		//echo $provider;
		if(!empty($provider))
			model('Xdata')->delete('sms:'.$provider);
		$this->assign('jumpUrl', U('/Global/sms'));
		$this->success('删除成功');
	}
	
	/** 系统配置 - 附件配置 **/
	
	public function attachConfig() {
		if ($_POST) {
			$_POST['attach_path_rule']		 = t($_POST['attach_path_rule']);
			$_POST['attach_max_size']		 = floatval($_POST['attach_max_size']);
			$_POST['attach_allow_extension'] = t($_POST['attach_allow_extension']);
			$this->assign('jumpUrl', U('/Global/attachConfig'));
			if ( model('Xdata')->lput('attach', $_POST) )
				$this->success('保存成功');
			else
				$this->error('保存失败');
			
		}else {
			$data = model('Xdata')->lget('attach');
			$this->assign($data);
			$this->display();
		}
	}
	
	/** 系统配置 - 平台配置 **/

	public function platform() {
		if($_POST) {
			foreach($_POST as $k => $v)
				$_POST[$k] = t($v);

			$this->assign('jumpUrl', U('/Global/platform'));
			if( model('Xdata')->lput('platform', $_POST) )
				$this->success('保存成功');
			else
				$this->error('保存失败');
		}else {
			$this->assign( model('Xdata')->lget('platform') );
			$this->display();
		}
	}

	/** 系统配置 - 文章配置 **/
	public function document() {
		if(!empty($_REQUEST['sort_id']))$ary['sort_id'] = $_REQUEST['sort_id'];
		//dump($ary);
		$order = '`sort_id` ASC,`display_order` ASC,`document_id` ASC';
		$pagedata = M('document')->kPageQuery($ary,$order,20);
		//dump($pagedata);
		$this->assign('pagedata', $pagedata);
		$this->assignRequestParams();
		$this->display();
	}

	public function addDocument() {
		$this->assign('type', 'add');
		$this->display('editDocument');
	}

	public function editDocument() {
		$map['document_id'] = intval($_GET['id']);
		$document = M('document')->where($map)->find();
		if ( empty($document) )
			$this->error('该文章不存在');
		$this->assign($document);

		$this->assign('type', 'edit');
		$this->display();
	}

	public function doEditDocument() {
		if ( ($_POST['document_id'] = intval($_POST['document_id'])) <= 0 )
			unset($_POST['document_id']);

		// 格式化数据
		$_POST['title']			= t($_POST['title']);
		$_POST['content']		= stripslashes($_POST['content']);
		$_POST['is_active']		= intval($_POST['is_active']);
		$_POST['is_on_footer']	= intval($_POST['is_on_footer']);
		$_POST['last_editor_id']= $this->mid;
		$_POST['mtime']			= time();
		//echo $_POST['content'];
		//echo stripslashes($_POST['content']);
		if ( !isset($_POST['document_id']) ) {
			// 新建文章
			$_POST['author_id']	= $this->mid;
			$_POST['ctime']		= $_POST['mtime'];
		}

		// 数据检查
		if ( empty($_POST['title']) )
			$this->error('标题不能为空');
		
		// 提交
		$res = isset($_POST['document_id']) ? M('document')->save($_POST) : M('document')->add($_POST);

		if($res) {
			if ( isset($_POST['document_id']) ) {
				$this->assign('jumpUrl', U('/Global/document'));
			}else {
				// 为排序方便, 新建完毕后, 将display_order设置为ad_id
				M('document')->where("`document_id`=$res")->setField('display_order', $res);
				$this->assign('jumpUrl', U('/Global/addDocument'));
			}
			$this->success('保存成功');
		}else {
			$this->error('保存失败');
		}
	}

	public function doDeleteDocument() {
		if( empty($_POST['ids']) ) {
			echo 0;
			exit ;
		}
		$map['document_id'] = array('in', t($_POST['ids']));
		echo M('document')->where($map)->delete() ? '1' : '0';
	}

	public function doDocumentOrder() {
		$_POST['document_id']	= intval($_POST['document_id']);
		$_POST['baseid']		= intval($_POST['baseid']);
		if ( $_POST['document_id'] <= 0 || $_POST['baseid'] <= 0 ) {
			echo 0;
			exit;
		}

		// 获取详情
		$map['document_id'] = array('in', array($_POST['document_id'], $_POST['baseid']));
		$res = M('document')->where($map)->field('document_id,display_order')->findAll();
		if ( count($res) < 2 ) {
			echo 0;
			exit;
		}

		//转为结果集为array('id'=>'order')的格式
    	foreach($res as $v) {
    		$order[$v['document_id']] = intval($v['display_order']);
    	}
    	unset($res);

    	//交换order值
    	$res = 		   M('document')->where('`document_id`=' . $_POST['document_id'])->setField(  'display_order', $order[$_POST['baseid']] );
    	$res = $res && M('document')->where('`document_id`=' . $_POST['baseid'])->setField( 'display_order', $order[$_POST['document_id']]  );

    	if($res) echo 1;
    	else	 echo 0;
	}
}